import requests
from requests import exceptions
import execjs


def API_query_addr(addr):

    if addr == "127.0.0.1":
        return {'city': '未知', 'province': '未知'}


    url = f"http://ip.ws.126.net/ipquery?ip={addr}"
    try:
        ret = requests.get(url=url,timeout=3)
        if ret.status_code ==200:
            jsdoc = execjs.compile(ret.text)
            ret = jsdoc.eval("localAddress")
            return ret
        else:
            return False
    except exceptions as e:
        print(e)
        return {'city': '未知', 'province': '未知'}


